package com.molyfun.parents.activity.msg;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.bean.GetRefundInfoResponse;
import com.molyfun.parents.rest.bean.MsgListResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RefundDetailActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.content_title)
    TextView mContentTitle;
    @InjectView(R.id.content)
    TextView mContent;
    @InjectView(R.id.sign_up_organization_name)
    TextView mOrganizationName;
    @InjectView(R.id.sign_up_date)
    TextView mEnrolDate;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;

    private MsgListResponse.MsgBean mMsgBean;

    public static void startActivity(Context context, MsgListResponse.MsgBean msgBean) {
        Intent intent = new Intent();
        intent.putExtra(Const.MSG_BEAN, msgBean);
        intent.setClass(context, RefundDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_detail);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        getSupportActionBar().setTitle("");
        mMsgBean = getIntent().getParcelableExtra(Const.MSG_BEAN);
        mBack.setOnClickListener(this);
        mBaseTitle.setText("退款通知");
        if(mMsgBean != null){
            mContentTitle.setText(mMsgBean.getTheme());
            mOrganizationName.setText(mMsgBean.getDepartname());
            mEnrolDate.setText(TimeUtils.getDateByTimeMillis(mMsgBean.getCreatedatetime()));
            getMsgDetail();
        }else{
            finish();
        }
    }

    private void getMsgDetail() {
        Call<GetRefundInfoResponse> responseCall = MFPApplication.getInstance().getRestClient().getRefundInfo(mMsgBean.getDetailid());
        responseCall.enqueue(new Callback<GetRefundInfoResponse>() {
            @Override
            public void onResponse(Call<GetRefundInfoResponse> call, Response<GetRefundInfoResponse> response) {
                if (response.body() != null && response.body().getData() != null) {

                    if(!TextUtils.isEmpty(mMsgBean.getCode()) && (mMsgBean.getCode().equals("DDTKSB"))){

                        StringBuilder sb = new StringBuilder();
//                        sb.append("退款金额:");
//                        sb.append(response.body().getData().getAmount());
//                        sb.append("\n");
                        sb.append("拒绝原因:");
                        sb.append(response.body().getData().getRejectreason());
                        sb.append("\n");
                        sb.append("(如需再次退款，请到订单中心再次申请)");
                        mContent.setText(sb.toString());
                    }else{
                        StringBuilder sb = new StringBuilder();
                        sb.append("退款金额:");
                        sb.append(response.body().getData().getAmount());
                        sb.append("\n");
                        sb.append("退款原因:");
                        sb.append(response.body().getData().getRefundremark());
                        sb.append("\n");
                        mContent.setText(sb.toString());
                    }


//                    if (!TextUtils.isEmpty(response.body().getData().getRejectreason())) {
//                        mContent.setText(response.body().getData().getRejectreason() + " 如需再次退款，请到订单中心再次申请");
//                    } else {
//                        mContent.setText(response.body().getData().getRefundreason());
//                    }
                }
            }

            @Override
            public void onFailure(Call<GetRefundInfoResponse> call, Throwable t) {
                toast("请求失败");
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }

    }

}

